home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb / sprite / RCS / sun3-dep.c,v < prev    next >
Encoding:
Text File  |  1990-12-10  |  16.5 KB  |  651 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     90.12.09.22.36.22;  author rab;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     90.11.19.17.16.59;  author rab;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @@
  22.  
  23.  
  24. 1.2
  25. log
  26. @Changes for Sprite. (Mike checking in for Bob.)
  27. @
  28. text
  29. @/* Machine-dependent code which would otherwise be in inflow.c and core.c,
  30.    for GDB, the GNU debugger.
  31.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  32.  
  33. This file is part of GDB.
  34.  
  35. GDB is free software; you can redistribute it and/or modify
  36. it under the terms of the GNU General Public License as published by
  37. the Free Software Foundation; either version 1, or (at your option)
  38. any later version.
  39.  
  40. GDB is distributed in the hope that it will be useful,
  41. but WITHOUT ANY WARRANTY; without even the implied warranty of
  42. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  43. GNU General Public License for more details.
  44.  
  45. You should have received a copy of the GNU General Public License
  46. along with GDB; see the file COPYING.  If not, write to
  47. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  48.  
  49. #include <stdio.h>
  50. #include "defs.h"
  51. #include "param.h"
  52. #include "frame.h"
  53. #include "inferior.h"
  54.  
  55. #include <sys/param.h>
  56. #include <sys/dir.h>
  57. #include <sys/user.h>
  58. #include <signal.h>
  59. #include <sys/ioctl.h>
  60. #include <fcntl.h>
  61.  
  62. #include <sys/ptrace.h>
  63. #include <sun3.md/reg.h>
  64.  
  65. #include <a.out.h>
  66. #include <sys/file.h>
  67. #include <sys/stat.h>
  68. #include <sys/core.h>
  69.  
  70. extern int errno;
  71. extern int attach_flag;
  72.  
  73. /* This function simply calls ptrace with the given arguments.  
  74.    It exists so that all calls to ptrace are isolated in this 
  75.    machine-dependent file. */
  76. int
  77. call_ptrace (request, pid, arg3, arg4)
  78.      int request, pid, arg3, arg4;
  79. {
  80.   return ptrace (request, pid, arg3, arg4);
  81. }
  82.  
  83. kill_inferior ()
  84. {
  85.   if (remote_debugging)
  86.     return;
  87.   if (inferior_pid == 0)
  88.     return;
  89.   ptrace (8, inferior_pid, 0, 0);
  90.   wait (0);
  91.   inferior_died ();
  92. }
  93.  
  94. /* This is used when GDB is exiting.  It gives less chance of error.*/
  95.  
  96. kill_inferior_fast ()
  97. {
  98.   if (remote_debugging)
  99.     return;
  100.   if (inferior_pid == 0)
  101.     return;
  102.   ptrace (8, inferior_pid, 0, 0);
  103.   wait (0);
  104. }
  105.  
  106. /* Resume execution of the inferior process.
  107.    If STEP is nonzero, single-step it.
  108.    If SIGNAL is nonzero, give it that signal.  */
  109.  
  110. void
  111. resume (step, signal)
  112.      int step;
  113.      int signal;
  114. {
  115.   errno = 0;
  116.   if (remote_debugging)
  117.     remote_resume (step, signal);
  118.   else
  119.     {
  120.       ptrace (step ? 9 : 7, inferior_pid, 1, signal);
  121.       if (errno)
  122.     perror_with_name ("ptrace");
  123.     }
  124. }
  125.  
  126. #ifdef ATTACH_DETACH
  127.  
  128. /* Start debugging the process whose number is PID.  */
  129.  
  130. attach (pid)
  131.      int pid;
  132. {
  133.   errno = 0;
  134.   ptrace (PTRACE_ATTACH, pid, 0, 0);
  135.   if (errno)
  136.     perror_with_name ("ptrace");
  137.   attach_flag = 1;
  138.   return pid;
  139. }
  140.  
  141. /* Stop debugging the process whose number is PID
  142.    and continue it with signal number SIGNAL.
  143.    SIGNAL = 0 means just continue it.  */
  144.  
  145. void
  146. detach (signal)
  147.      int signal;
  148. {
  149.   errno = 0;
  150.   ptrace (PTRACE_DETACH, inferior_pid, 1, signal);
  151.   if (errno)
  152.     perror_with_name ("ptrace");
  153.   attach_flag = 0;
  154. }
  155. #endif /* ATTACH_DETACH */
  156.  
  157. void
  158. fetch_inferior_registers ()
  159. {
  160.   struct regs inferior_registers;
  161. #ifdef FP0_REGNUM
  162.   struct fp_status inferior_fp_registers;
  163. #endif
  164.   extern char registers[];
  165.  
  166.   if (remote_debugging)
  167.     remote_fetch_registers (registers);
  168.   else
  169.     {
  170.       ptrace (PTRACE_GETREGS, inferior_pid, &inferior_registers);
  171. #ifdef FP0_REGNUM
  172.       ptrace (PTRACE_GETFPREGS, inferior_pid, &inferior_fp_registers);
  173. #endif 
  174.  
  175.       bcopy (&inferior_registers, registers, 16 * 4);
  176. #ifdef FP0_REGNUM
  177.       bcopy (&inferior_fp_registers, ®isters[REGISTER_BYTE (FP0_REGNUM)],
  178.          sizeof inferior_fp_registers.fps_regs);
  179. #endif 
  180.       *(int *)®isters[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps;
  181.       *(int *)®isters[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc;
  182. #ifdef FP0_REGNUM
  183.       bcopy (&inferior_fp_registers.fps_control,
  184.          ®isters[REGISTER_BYTE (FPC_REGNUM)],
  185.          sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs);
  186. #endif 
  187.     }
  188. }
  189.  
  190. /* Store our register values back into the inferior.
  191.    If REGNO is -1, do this for all registers.
  192.    Otherwise, REGNO specifies which register (so we can save time).  */
  193.  
  194. store_inferior_registers (regno)
  195.      int regno;
  196. {
  197.   struct regs inferior_registers;
  198.   struct fp_status inferior_fp_registers;
  199.   extern char registers[];
  200.  
  201.   if (remote_debugging)
  202.     remote_store_registers (registers);
  203.   else
  204.     {
  205.       bcopy (registers, &inferior_registers, 16 * 4);
  206. #ifdef FP0_REGNUM
  207.       bcopy (®isters[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers,
  208.          sizeof inferior_fp_registers.fps_regs);
  209. #endif
  210.       inferior_registers.r_ps = *(int *)®isters[REGISTER_BYTE (PS_REGNUM)];
  211.       inferior_registers.r_pc = *(int *)®isters[REGISTER_BYTE (PC_REGNUM)];
  212.  
  213. #ifdef FP0_REGNUM
  214.       bcopy (®isters[REGISTER_BYTE (FPC_REGNUM)],
  215.          &inferior_fp_registers.fps_control,
  216.          sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs);
  217. #endif
  218.  
  219.       ptrace (PTRACE_SETREGS, inferior_pid, &inferior_registers);
  220. #if FP0_REGNUM
  221.       ptrace (PTRACE_SETFPREGS, inferior_pid, &inferior_fp_registers);
  222. #endif
  223.     }
  224. }
  225.  
  226. /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
  227.    in the NEW_SUN_PTRACE case.
  228.    It ought to be straightforward.  But it appears that writing did
  229.    not write the data that I specified.  I cannot understand where
  230.    it got the data that it actually did write.  */
  231.  
  232. /* Copy LEN bytes from inferior's memory starting at MEMADDR
  233.    to debugger memory starting at MYADDR. 
  234.    On failure (cannot read from inferior, usually because address is out
  235.    of bounds) returns the value of errno. */
  236.  
  237. int
  238. read_inferior_memory (memaddr, myaddr, len)
  239.      CORE_ADDR memaddr;
  240.      char *myaddr;
  241.      int len;
  242. {
  243.   register int i;
  244.   /* Round starting address down to longword boundary.  */
  245.   register CORE_ADDR addr = memaddr & - sizeof (int);
  246.   /* Round ending address up; get number of longwords that makes.  */
  247.   register int count
  248.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  249.   /* Allocate buffer of that many longwords.  */
  250.   register int *buffer = (int *) alloca (count * sizeof (int));
  251.   extern int errno;
  252.  
  253.   /* Read all the longwords */
  254.   for (i = 0; i < count; i++, addr += sizeof (int))
  255.     {
  256.       errno = 0;
  257.       if (remote_debugging)
  258.     buffer[i] = remote_fetch_word (addr);
  259.       else
  260.     buffer[i] = ptrace (1, inferior_pid, addr, 0);
  261.       if (errno)
  262.     return errno;
  263.     }
  264.  
  265.   /* Copy appropriate bytes out of the buffer.  */
  266.   bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
  267.   return 0;
  268. }
  269.  
  270. /* Copy LEN bytes of data from debugger memory at MYADDR
  271.    to inferior's memory at MEMADDR.
  272.    On failure (cannot write the inferior)
  273.    returns the value of errno.  */
  274.  
  275. int
  276. write_inferior_memory (memaddr, myaddr, len)
  277.      CORE_ADDR memaddr;
  278.      char *myaddr;
  279.      int len;
  280. {
  281.   register int i;
  282.   /* Round starting address down to longword boundary.  */
  283.   register CORE_ADDR addr = memaddr & - sizeof (int);
  284.   /* Round ending address up; get number of longwords that makes.  */
  285.   register int count
  286.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  287.   /* Allocate buffer of that many longwords.  */
  288.   register int *buffer = (int *) alloca (count * sizeof (int));
  289.   extern int errno;
  290.  
  291.   /* Fill start and end extra bytes of buffer with existing memory data.  */
  292.  
  293.   if (remote_debugging)
  294.     buffer[0] = remote_fetch_word (addr);
  295.   else
  296.     buffer[0] = ptrace (1, inferior_pid, addr, 0);
  297.  
  298.   if (count > 1)
  299.     {
  300.       if (remote_debugging)
  301.     buffer[count - 1]
  302.       = remote_fetch_word (addr + (count - 1) * sizeof (int));
  303.       else
  304.     buffer[count - 1]
  305.       = ptrace (1, inferior_pid,
  306.             addr + (count - 1) * sizeof (int), 0);
  307.     }
  308.  
  309.   /* Copy data to be written over corresponding part of buffer */
  310.  
  311.   bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
  312.  
  313.   /* Write the entire buffer.  */
  314.  
  315.   for (i = 0; i < count; i++, addr += sizeof (int))
  316.     {
  317.       errno = 0;
  318.       if (remote_debugging)
  319.     remote_store_word (addr, buffer[i]);
  320.       else
  321.     ptrace (4, inferior_pid, addr, buffer[i]);
  322.       if (errno)
  323.     return errno;
  324.     }
  325.  
  326.   return 0;
  327. }
  328.  
  329.  
  330. /* Machine-dependent code which would otherwise be in core.c */
  331. /* Work with core dump and executable files, for GDB. */
  332.  
  333. #ifndef N_TXTADDR
  334. #define N_TXTADDR(hdr) 0
  335. #endif /* no N_TXTADDR */
  336.  
  337. #ifndef N_DATADDR
  338. #define N_DATADDR(hdr) hdr.a_text
  339. #endif /* no N_DATADDR */
  340.  
  341. /* Non-zero if this is an object (.o) file, rather than an executable.
  342.    Distinguishing between the two is rarely necessary (and seems like
  343.    a hack, but there is no other way to get the text and data
  344.    addresses--N_TXTADDR should probably take care of
  345.    this, but it doesn't).  */
  346. /* This definition will not work
  347.    if someone decides to make ld preserve relocation info.  */
  348. #define IS_OBJECT_FILE(hdr) (hdr.a_trsize != 0)
  349.   
  350. /* Make COFF and non-COFF names for things a little more compatible
  351.    to reduce conditionals later.  */
  352.  
  353. #ifdef COFF_FORMAT
  354. #define a_magic magic
  355. #endif
  356.  
  357. #ifndef COFF_FORMAT
  358. #ifndef AOUTHDR
  359. #define AOUTHDR struct exec
  360. #endif
  361. #endif
  362.  
  363. extern char *sys_siglist[];
  364.  
  365. /* Hook for `exec_file_command' command to call.  */
  366.  
  367. extern void (*exec_file_display_hook) ();
  368.    
  369. /* File names of core file and executable file.  */
  370.  
  371. extern char *corefile;
  372. extern char *execfile;
  373.  
  374. /* Descriptors on which core file and executable file are open.
  375.    Note that the execchan is closed when an inferior is created
  376.    and reopened if the inferior dies or is killed.  */
  377.  
  378. extern int corechan;
  379. extern int execchan;
  380.  
  381. /* Last modification time of executable file.
  382.    Also used in source.c to compare against mtime of a source file.  */
  383.  
  384. extern int exec_mtime;
  385.  
  386. /* Virtual addresses of bounds of the two areas of memory in the core file.  */
  387.  
  388. extern CORE_ADDR data_start;
  389. extern CORE_ADDR data_end;
  390. extern CORE_ADDR stack_start;
  391. extern CORE_ADDR stack_end;
  392.  
  393. /* Virtual addresses of bounds of two areas of memory in the exec file.
  394.    Note that the data area in the exec file is used only when there is no core file.  */
  395.  
  396. extern CORE_ADDR text_start;
  397. extern CORE_ADDR text_end;
  398.  
  399. extern CORE_ADDR exec_data_start;
  400. extern CORE_ADDR exec_data_end;
  401.  
  402. /* Address in executable file of start of text area data.  */
  403.  
  404. extern int text_offset;
  405.  
  406. /* Address in executable file of start of data area data.  */
  407.  
  408. extern int exec_data_offset;
  409.  
  410. /* Address in core file of start of data area data.  */
  411.  
  412. extern int data_offset;
  413.  
  414. /* Address in core file of start of stack area data.  */
  415.  
  416. extern int stack_offset;
  417.   
  418. #ifdef COFF_FORMAT
  419. /* various coff data structures */
  420.  
  421. extern FILHDR file_hdr;
  422. extern SCNHDR text_hdr;
  423. extern SCNHDR data_hdr;
  424.  
  425. #endif /* not COFF_FORMAT */
  426.  
  427. /* a.out header saved in core file.  */
  428.   
  429. extern AOUTHDR core_aouthdr;
  430.  
  431. /* a.out header of exec file.  */
  432.  
  433. extern AOUTHDR exec_aouthdr;
  434.  
  435. extern void validate_files ();
  436.  
  437. core_file_command (filename, from_tty)
  438.      char *filename;
  439.      int from_tty;
  440. {
  441.   int val;
  442.   extern char registers[];
  443.  
  444.   /* Discard all vestiges of any previous core file
  445.      and mark data and stack spaces as empty.  */
  446.  
  447.   if (corefile)
  448.     free (corefile);
  449.   corefile = 0;
  450.  
  451.   if (corechan >= 0)
  452.     close (corechan);
  453.   corechan = -1;
  454.  
  455.   data_start = 0;
  456.   data_end = 0;
  457.   stack_start = STACK_END_ADDR;
  458.   stack_end = STACK_END_ADDR;
  459.  
  460.   /* Now, if a new core file was specified, open it and digest it.  */
  461.  
  462.   if (filename)
  463.     {
  464.       filename = tilde_expand (filename);
  465.       make_cleanup (free, filename);
  466.       
  467.       if (have_inferior_p ())
  468.     error ("To look at a core file, you must kill the inferior with \"kill\".");
  469.       corechan = open (filename, O_RDONLY, 0);
  470.       if (corechan < 0)
  471.     perror_with_name (filename);
  472.  
  473.       {
  474.     struct core corestr;
  475.  
  476.     val = myread (corechan, &corestr, sizeof corestr);
  477.     if (val < 0)
  478.       perror_with_name (filename);
  479.     if (corestr.c_magic != CORE_MAGIC)
  480.       error ("\"%s\" does not appear to be a core dump file (magic 0x%x, expected 0x%x)",
  481.          filename, corestr.c_magic, (int) CORE_MAGIC);
  482.     else if (sizeof (struct core) != corestr.c_len)
  483.       error ("\"%s\" has an invalid struct core length (%d, expected %d)",
  484.          filename, corestr.c_len, (int) sizeof (struct core));
  485.  
  486.     data_start = exec_data_start;
  487.     data_end = data_start + corestr.c_dsize;
  488.     stack_start = stack_end - corestr.c_ssize;
  489.     data_offset = sizeof corestr;
  490.     stack_offset = sizeof corestr + corestr.c_dsize;
  491.  
  492.     bcopy (&corestr.c_regs, registers, 16 * 4);
  493.     *(int *)®isters[REGISTER_BYTE (PS_REGNUM)] = corestr.c_regs.r_ps;
  494.     *(int *)®isters[REGISTER_BYTE (PC_REGNUM)] = corestr.c_regs.r_pc;
  495. #ifdef FP0_REGNUM
  496. #ifdef FPU
  497.      bcopy (corestr.c_fpu.f_fpstatus.fps_regs,
  498.             ®isters[REGISTER_BYTE (FP0_REGNUM)],
  499.             sizeof corestr.c_fpu.f_fpstatus.fps_regs);
  500.      bcopy (&corestr.c_fpu.f_fpstatus.fps_control,
  501.             ®isters[REGISTER_BYTE (FPC_REGNUM)],
  502.             sizeof corestr.c_fpu.f_fpstatus - sizeof corestr.c_fpu.f_fpstatus.fps_regs);
  503. #else
  504.     bcopy (corestr.c_fpstatus.fps_regs,
  505.            ®isters[REGISTER_BYTE (FP0_REGNUM)],
  506.            sizeof corestr.c_fpstatus.fps_regs);
  507.     bcopy (&corestr.c_fpstatus.fps_control,
  508.            ®isters[REGISTER_BYTE (FPC_REGNUM)],
  509.            sizeof corestr.c_fpstatus - sizeof corestr.c_fpstatus.fps_regs);
  510. #endif
  511. #endif
  512.     bcopy (&corestr.c_aouthdr, &core_aouthdr, sizeof (struct exec));
  513.  
  514.     printf ("Core file is from \"%s\".\n", corestr.c_cmdname);
  515.     if (corestr.c_signo > 0)
  516.       printf ("Program terminated with signal %d, %s.\n",
  517.             corestr.c_signo,
  518.             corestr.c_signo < NSIG
  519.             ? sys_siglist[corestr.c_signo]
  520.             : "(undocumented)");
  521.       }
  522.       if (filename[0] == '/')
  523.     corefile = savestring (filename, strlen (filename));
  524.       else
  525.     {
  526.       corefile = concat (current_directory, "/", filename);
  527.     }
  528.  
  529.       set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  530.                         read_pc ()));
  531.       select_frame (get_current_frame (), 0);
  532.       validate_files ();
  533.     }
  534.   else if (from_tty)
  535.     printf ("No core file now.\n");
  536. }
  537.  
  538. exec_file_command (filename, from_tty)
  539.      char *filename;
  540.      int from_tty;
  541. {
  542.   int val;
  543.  
  544.   /* Eliminate all traces of old exec file.
  545.      Mark text segment as empty.  */
  546.  
  547.   if (execfile)
  548.     free (execfile);
  549.   execfile = 0;
  550.   data_start = 0;
  551.   data_end -= exec_data_start;
  552.   text_start = 0;
  553.   text_end = 0;
  554.   exec_data_start = 0;
  555.   exec_data_end = 0;
  556.   if (execchan >= 0)
  557.     close (execchan);
  558.   execchan = -1;
  559.  
  560.   /* Now open and digest the file the user requested, if any.  */
  561.  
  562.   if (filename)
  563.     {
  564.       filename = tilde_expand (filename);
  565.       make_cleanup (free, filename);
  566.       
  567.       execchan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
  568.             &execfile);
  569.       if (execchan < 0)
  570.     perror_with_name (filename);
  571.  
  572. #ifdef COFF_FORMAT
  573.       {
  574.     int aout_hdrsize;
  575.     int num_sections;
  576.  
  577.     if (read_file_hdr (execchan, &file_hdr) < 0)
  578.       error ("\"%s\": not in executable format.", execfile);
  579.  
  580.     aout_hdrsize = file_hdr.f_opthdr;
  581.     num_sections = file_hdr.f_nscns;
  582.  
  583.     if (read_aout_hdr (execchan, &exec_aouthdr, aout_hdrsize) < 0)
  584.       error ("\"%s\": can't read optional aouthdr", execfile);
  585.  
  586.     if (read_section_hdr (execchan, _TEXT, &text_hdr, num_sections,
  587.                   aout_hdrsize) < 0)
  588.       error ("\"%s\": can't read text section header", execfile);
  589.  
  590.     if (read_section_hdr (execchan, _DATA, &data_hdr, num_sections,
  591.                   aout_hdrsize) < 0)
  592.       error ("\"%s\": can't read data section header", execfile);
  593.  
  594.     text_start = exec_aouthdr.text_start;
  595.     text_end = text_start + exec_aouthdr.tsize;
  596.     text_offset = text_hdr.s_scnptr;
  597.     exec_data_start = exec_aouthdr.data_start;
  598.     exec_data_end = exec_data_start + exec_aouthdr.dsize;
  599.     exec_data_offset = data_hdr.s_scnptr;
  600.     data_start = exec_data_start;
  601.     data_end += exec_data_start;
  602.     exec_mtime = file_hdr.f_timdat;
  603.       }
  604. #else /* not COFF_FORMAT */
  605.       {
  606.     struct stat st_exec;
  607.     val = myread (execchan, &exec_aouthdr, sizeof (AOUTHDR));
  608.  
  609.     if (val < 0)
  610.       perror_with_name (filename);
  611.  
  612.     text_start =
  613.       IS_OBJECT_FILE (exec_aouthdr) ? 0 : N_TXTADDR (exec_aouthdr);
  614.         exec_data_start = IS_OBJECT_FILE (exec_aouthdr)
  615.       ? exec_aouthdr.a_text : N_DATADDR (exec_aouthdr);
  616.     text_offset = N_TXTOFF (exec_aouthdr);
  617.     exec_data_offset = N_TXTOFF (exec_aouthdr) + exec_aouthdr.a_text;
  618.  
  619.     text_end = text_start + exec_aouthdr.a_text;
  620.         exec_data_end = exec_data_start + exec_aouthdr.a_data;
  621.     data_start = exec_data_start;
  622.     data_end += exec_data_start;
  623.  
  624.     fstat (execchan, &st_exec);
  625.     exec_mtime = st_exec.st_mtime;
  626.       }
  627. #endif /* not COFF_FORMAT */
  628.  
  629.       validate_files ();
  630.     }
  631.   else if (from_tty)
  632.     printf ("No exec file now.\n");
  633.  
  634.   /* Tell display code (if any) about the changed file name.  */
  635.   if (exec_file_display_hook)
  636.     (*exec_file_display_hook) (filename);
  637. }
  638.  
  639. @
  640.  
  641.  
  642. 1.1
  643. log
  644. @Initial revision
  645. @
  646. text
  647. @d35 1
  648. a35 1
  649. #include <machine/reg.h>
  650. @
  651.